/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IResolvedResult;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;

import java.util.List;
import java.util.Map;

/**
 * ClientUser object for the 'p4 resolved' command.
 * 
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class Resolved extends AbstractPerforceCommand implements IPerforceCommand {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the command's status. */
  private MultiStatus status;
  /** Stores the result of the 'p4 resolved' operation. */
  private List<Result> resultList = Lists.newArrayList();

  // --------------------------------------------------------------- Public
  // Static Factory Methods

  /**
   * Convenience method for creating instances of 'Resolved'.
   * 
   * @return the new Resolved instance.
   */
  public static final Resolved newInstance() {
    MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
    return new Resolved(status);
  }

  // ------------------------------------------------------------------------
  // Private Constructors

  /**
   * Constructor for Resolved.
   * 
   * @param status the MultiStatus object.
   */
  private Resolved(MultiStatus status) {
    super(status);
    this.status = status;
  }

  // -------------------------------------------------------- Public Methods
  // (IClientUserDelegate)

  @Override
  public void outputStat(Map<String, ?> data) {
    String opt3 = "";

    if (data.get("how").equals("branch from")) //$NON-NLS-1$
      opt3 = ResourceSyncInfo.ACTION_BRANCH;

    String depotFilename = (String) data.get("fromFile"); //$NON-NLS-1$
    String filename = (String) data.get("toFile"); //$NON-NLS-1$

    Result result = new Result(depotFilename, filename, ResourceSyncInfo.ACTION_BRANCH);
    resultList.add(result);

    return;
  }

  @Override
  public void outputInfo(char level, String infoString) {
    if (level != '0') {
      return;
    }

    int start = infoString.indexOf(" - branch from "); //$NON-NLS-1$
    if (start != -1) {
      String depotFilename = infoString.substring(start + 15);
      String filename = infoString.substring(0, start);

      Result result = new Result(depotFilename, filename, ResourceSyncInfo.ACTION_BRANCH);
      resultList.add(result);

      return;
    }

    start = infoString.indexOf(" - "); //$NON-NLS-1$
    if (start != -1) {
      int realStart = infoString.indexOf("//", start + 3); //$NON-NLS-1$
      String depotFilename = infoString.substring(realStart);
      String filename = infoString.substring(0, start);

      Result result = new Result(depotFilename, filename, ""); //$NON-NLS-1$
      resultList.add(result);

      return;
    }

    super.outputInfo(level, infoString);
  }

  // ----------------------------------------------------------- Public Methods
  // (IPerforceCommand)

  public String getCommand() {
    return "resolved"; //$NON-NLS-1$
  }

  @Override
  public IStatus getStatus() {
    return status;
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Returns the operation's result.
   * 
   * @return the result of 'p4 resolved'.
   */
  public IResolvedResult[] getResult() {
    return resultList.toArray(new IResolvedResult[resultList.size()]);
  }

  // ----------------------------------------------------------------------
  // Private Nested Classes

  /** Class representing the operations result. */
  private static class Result implements IResolvedResult {
    // -----------------------------------------------------------------------
    // Private Variables

    /** Stores the depot filename */
    private String depotFilename;
    /** Stores the location */
    private IPath location;
    /** Stores the action. Currently only "branch" or "" */
    private String action;

    // ---------------------------------------------------------------------
    // Public Constructors

    /**
     * Constructor for Result.
     * 
     * @param theDepotFilename the depot filename.
     * @param theFilename the filename.
     * @param theAction the action.
     */
    public Result(String theDepotFilename, String theFilename, String theAction) {
      int hashIndex = theDepotFilename.indexOf('#');
      if (hashIndex > 0)
        depotFilename = theDepotFilename.substring(0, hashIndex);
      else
        depotFilename = theDepotFilename;
      location = new Path(theFilename);
      action = theAction;
    }

    //--------------------------------------------------------------------------
    // Public Methods

    /** @see net.sourceforge.perforce.core.api.IResolvedResult#getDepotFilename() */
    public String getDepotFilename() {
      return depotFilename;
    }

    /** @see net.sourceforge.perforce.core.api.IResolvedResult#getLocation() */
    public IPath getLocation() {
      return location;
    }

    /** @see net.sourceforge.perforce.core.api.IResolvedResult#getAction() */
    public String getAction() {
      return action;
    }
  }
}
